package com.luo.chengrui.module.sa.controller.admin.matrix;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;

import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.luo.chengrui.module.sa.controller.admin.matrix.vo.*;
import com.luo.chengrui.module.sa.dal.dataobject.matrix.OporgManagerDefineDO;
import com.luo.chengrui.module.sa.convert.matrix.OporgManagerDefineConvert;
import com.luo.chengrui.module.sa.service.matrix.OporgManagerDefineService;

@Tag(name = "管理后台 - 组织管理岗定义")
@RestController
@RequestMapping("/sa/oporg-manager-define")
@Validated
public class OporgManagerDefineController {

    @Resource
    private OporgManagerDefineService oporgManagerDefineService;

    @PostMapping("/create")
    @Operation(summary = "创建组织管理岗定义")
    @PreAuthorize("@ss.hasPermission('sa:oporg-manager-define:create')")
    public CommonResult<String> createOporgManagerDefine(@Valid @RequestBody OporgManagerDefineCreateReqVO createReqVO) {
        return success(oporgManagerDefineService.createOporgManagerDefine(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新组织管理岗定义")
    @PreAuthorize("@ss.hasPermission('sa:oporg-manager-define:update')")
    public CommonResult<Boolean> updateOporgManagerDefine(@Valid @RequestBody OporgManagerDefineUpdateReqVO updateReqVO) {
        oporgManagerDefineService.updateOporgManagerDefine(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除组织管理岗定义")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('sa:oporg-manager-define:delete')")
    public CommonResult<Boolean> deleteOporgManagerDefine(@RequestParam("id") String id) {
        oporgManagerDefineService.deleteOporgManagerDefine(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得组织管理岗定义")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('sa:oporg-manager-define:query')")
    public CommonResult<OporgManagerDefineRespVO> getOporgManagerDefine(@RequestParam("id") String id) {
        OporgManagerDefineDO oporgManagerDefine = oporgManagerDefineService.getOporgManagerDefine(id);
        return success(OporgManagerDefineConvert.INSTANCE.convert(oporgManagerDefine));
    }

    @GetMapping("/list")
    @Operation(summary = "获得组织管理岗定义列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('sa:oporg-manager-define:query')")
    public CommonResult<List<OporgManagerDefineRespVO>> getOporgManagerDefineList(@RequestParam("ids") Collection<String> ids) {
        List<OporgManagerDefineDO> list = oporgManagerDefineService.getOporgManagerDefineList(ids);
        return success(OporgManagerDefineConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/all")
    @Operation(summary = "获得组织管理岗定义")
    @PreAuthorize("@ss.hasPermission('sa:oporg-manager-define:query')")
    public CommonResult<List<OporgManagerDefineRespVO>> getOporgManagerDefineAllList(@Valid OporgManagerDefineBaseVO vo) {
        List<OporgManagerDefineDO> list = oporgManagerDefineService.getOporgManagerDefineAllList(vo);
        return success(OporgManagerDefineConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得组织管理岗定义分页")
    @PreAuthorize("@ss.hasPermission('sa:oporg-manager-define:query')")
    public CommonResult<PageResult<OporgManagerDefineRespVO>> getOporgManagerDefinePage(@Valid OporgManagerDefinePageReqVO pageVO) {
        PageResult<OporgManagerDefineDO> pageResult = oporgManagerDefineService.getOporgManagerDefinePage(pageVO);
        return success(OporgManagerDefineConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出组织管理岗定义 Excel")
    @PreAuthorize("@ss.hasPermission('sa:oporg-manager-define:export')")
    @OperateLog(type = EXPORT)
    public void exportOporgManagerDefineExcel(@Valid OporgManagerDefineExportReqVO exportReqVO,
                                              HttpServletResponse response) throws IOException {
        List<OporgManagerDefineDO> list = oporgManagerDefineService.getOporgManagerDefineList(exportReqVO);
        // 导出 Excel
        List<OporgManagerDefineExcelVO> datas = OporgManagerDefineConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "组织管理岗定义.xls", "数据", OporgManagerDefineExcelVO.class, datas);
    }

}
